package com.zxkj.login.viewmodel.activity;

import android.app.Application;
import android.os.Handler;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;

import com.czy.basecommon.utils.base.BaseRepository;
import com.czy.basecommon.utils.base.BaseViewModel;
import com.czy.basecommon.utils.base.StateLiveData;
import com.zxkj.login.net.ResponseLogin;
import com.zxkj.login.repository.activity.LoginRepository;


public class LoginViewModel extends BaseViewModel<LoginRepository> {

  public   StateLiveData<String> data=new StateLiveData<>();
    public StateLiveData<ResponseLogin> login=new StateLiveData();

    public LoginViewModel(@NonNull Application application) {
        super(application);
    }





    public  void   login(String textEx, String ex){
       mRepository.login(textEx,ex,new BaseRepository.MyBaseDataCallBack() {
             @Override
             public void onDataSuccessCallBack(Object o) {
                 if (o!=null){
                     ResponseLogin responseLogin =(ResponseLogin)o;
                     login.postSuccess(responseLogin);
                 }else{
                     login.postFailure(404,"");
                 }



             }

             @Override
             public void onDataFailCallBack(Object o, Object t1) {
                 String code=(String)o;
                 int errorCode;
                try {
                     errorCode=Integer.parseInt(code);
                }catch (Exception e){
                    errorCode=404;
                }


                 login.postFailure(errorCode,t1.toString());
             }
         });

    }




    //测试

    public void upData(){


            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                    //模拟数据加载

                      data.postSuccess("1");


                }
            }, 1000);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                //模拟数据加载

                data.postSuccess("2");


            }
        }, 2000);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                //模拟数据加载

                data.postFailure(306,"错误l1");


            }
        }, 3000);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                //模拟数据加载

                data.postFailure(307,"错误l2");


            }
        }, 4000);


    }



}
